
 /* To change this license header, choose License Headers in Project Properties.
  To change this template file, choose Tools | Templates
  and open the template in the editor.*/
 
package com.seari.controller;

import com.seari.bean.MenuPrivilege;
import com.seari.pojo.DataGridBean;
import com.seari.pojo.MessageBean;
import com.seari.pojo.QueryMenuPrivilege;
import com.seari.pojo.QueryRolePrivilege;
import com.seari.service.MenuService;
import com.seari.utils.StringHelper;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author Touch
 */
@Controller
@RequestMapping("menu/")
public class MenuController
{
    @Autowired
    private MenuService menuService;
    
    
    /**
     * @param req
     * @return
     * 
     */
    @RequestMapping("getMenuResourceByMpid")
    @ResponseBody
    public DataGridBean<MenuPrivilege> getMenuResourceByMpid(HttpServletRequest req)
    {
    	String menuId = req.getParameter("mpid");
    	DataGridBean<MenuPrivilege> grid = new DataGridBean<MenuPrivilege>();
    	if(StringUtils.isNotBlank(menuId))
    	{
        List<MenuPrivilege> menuPrivileges = menuService.getMenuResourceByMpid(Integer.parseInt(menuId));
        grid.setRows(menuPrivileges);
        grid.setTotal(menuPrivileges.size());
    	}
    	return grid;
    }
    
    /**
     * @param req
     * @return
     * 
     */
    @RequestMapping("getPrivilegeByMpidsAndUrid")
    @ResponseBody
    public DataGridBean<MenuPrivilege> getPrivilegeByMpidsAndUrid(HttpServletRequest req)
    {
    	String urid = req.getParameter("urid");
    	String [] values = req.getParameterValues("mpids[]");
    	//String [] values = {"2","7"};
    	
    	
    	DataGridBean<MenuPrivilege> grid = new DataGridBean<MenuPrivilege>();
    	if(ArrayUtils.isNotEmpty(values) || StringUtils.isNotBlank(urid))
    	{
        	int [] mpids = new int[values.length];
        	for (int i = 0; i < values.length; i++) {
        		mpids[i] = Integer.parseInt(values[i]);
			}
    		QueryRolePrivilege qrp = new QueryRolePrivilege();
    		if(StringUtils.isNotBlank(urid)){
    		qrp.setUurid(Integer.parseInt(urid));
    		}
    		qrp.setUupids(mpids);
    		
            List<MenuPrivilege> menuPrivileges = menuService.getPrivilegeByMpidsAndUrid(qrp);
            
            grid.setRows(menuPrivileges);
            grid.setTotal(menuPrivileges.size());
    	}
    	return grid;
    }
    
    /**
     * @param req
     * @return
     * 
     */
    @RequestMapping("getNotHaveByMpidsAndUrid")
    @ResponseBody
    public DataGridBean<MenuPrivilege> getNotHaveByMpidsAndUrid(HttpServletRequest req)
    {
    	String urid = req.getParameter("urid");
    	String [] values = req.getParameterValues("mpids[]");
        String match = req.getParameter("match");
    	//String [] values = {"2","7"};
    	
    	DataGridBean<MenuPrivilege> grid = new DataGridBean<MenuPrivilege>();
    	if(ArrayUtils.isNotEmpty(values) || StringUtils.isNotBlank(urid))
    	{
        	int [] mpids = new int[values.length];
        	for (int i = 0; i < values.length; i++) {
        		mpids[i] = Integer.parseInt(values[i]);
			}
    		QueryRolePrivilege qrp = new QueryRolePrivilege();
    		if(StringUtils.isNotBlank(urid)){
    		qrp.setUurid(Integer.parseInt(urid));
    		}
    		qrp.setUupids(mpids);
    		qrp.setMatch(match);
    		
            List<MenuPrivilege> menuPrivileges = menuService.getNotHaveByMpidsAndUrid(qrp);
            
            grid.setRows(menuPrivileges);
            grid.setTotal(menuPrivileges.size());
    	}
    	return grid;
    }
    
    
    
    /**
     * @param req
     * @return
     * 
     */
    @RequestMapping("getMenuResources")
    @ResponseBody
    public DataGridBean<MenuPrivilege> getMenuResources(HttpServletRequest req)
    {
        DataGridBean<MenuPrivilege> grid = new DataGridBean<MenuPrivilege>();
        String currPage = req.getParameter("page");
        String pageSize = req.getParameter("limit");
        String match = req.getParameter("match");
        
        String urid = req.getParameter("urid");
        
        Integer count = 0;
        
        QueryMenuPrivilege qu = new QueryMenuPrivilege();
        if(StringUtils.isNotBlank(currPage) && StringUtils.isNotBlank(pageSize))
        {
        	qu.setStart((Integer.parseInt(currPage) - 1) * Integer.parseInt(pageSize));
        	qu.setLimit(Integer.parseInt(pageSize));

        }
        List<MenuPrivilege> menuPrivileges = null;
        if(StringUtils.isNotBlank(match)){
        	qu.setMatch(match);
            count = menuService.getMenuResourcesCount(qu);
        	menuPrivileges = menuService.menuSearch(qu);
        	grid.setTotal(count);
        	grid.setRows(menuPrivileges);
        	return grid;
        }
        if(StringUtils.isNotBlank(urid)){
            menuPrivileges = menuService.getMenuResources(qu);
            List<MenuPrivilege> mpidList = menuService.getRoleMenuByUupid(Integer.parseInt(urid));
            for (int i = 0; i < mpidList.size(); i++) {
            	for (int j = 0; j < menuPrivileges.size(); j++) {
                	if(menuPrivileges.get(j).getMpid() == mpidList.get(i).getMpid()){
                		menuPrivileges.get(j).setCheckArr("1");
                	}
				}
			}
            grid.setRows(menuPrivileges);
            grid.setTotal(menuPrivileges.size());
        	return grid;
        }
        
        menuPrivileges = menuService.getMenuResources(qu);
        grid.setRows(menuPrivileges);
        count = menuService.getMenuResourcesCount(qu);
        grid.setTotal(count);
    	 return grid;
    }
    
    
    
    @RequestMapping("addMenuResource")
    @ResponseBody
    public MessageBean addMenuResource(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String pmName = req.getParameter("pmName");
        String pmCode = req.getParameter("pmCode");
        String pmComment = req.getParameter("pmComment");
        String menuType = req.getParameter("menuType");
        String src = req.getParameter("src");
        String parentId = req.getParameter("parentId");
        
        if (StringUtils.isBlank(parentId)) {
			
        	parentId = "0";
		}
        
        if( StringUtils.isBlank(pmName) 
            || StringUtils.isBlank(pmCode)
            || StringUtils.isBlank(pmComment) 
            || StringUtils.isBlank(menuType))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else{
        	 MenuPrivilege menuPrivilege = new MenuPrivilege();
        	if(!StringUtils.isBlank(src)){
                QueryMenuPrivilege qup = new QueryMenuPrivilege();
                menuPrivilege.setSrc(src);
                qup.setSrc(src);
                int resultNum = menuService.checkMenuPrivilegeExist(qup);
                if(resultNum > 0 )
                {
                    mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                    mb.setMessage("地址名重复");
                    return mb;
                }
        		
        	}
           
            menuPrivilege.setPmName(pmName);
            menuPrivilege.setPmCode(pmCode);
            menuPrivilege.setPmComment(pmComment);
            menuPrivilege.setMenuType(menuType);
            menuPrivilege.setParentId(Integer.parseInt(parentId));
            
            int resultNum1 = menuService.addMenuResource(menuPrivilege);
            if(resultNum1 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("菜单权限添加失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("菜单权限添加成功");
            }
        }
        return mb;
    }
    
    
    @RequestMapping("updateMenuResource")
    @ResponseBody
    public MessageBean updateMenuResource(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
       // String parameter = req.getParameter("upid");
        String mpid = req.getParameter("mpid");
        String pmName = req.getParameter("pmName");
        String pmCode = req.getParameter("pmCode");
        String pmComment = req.getParameter("pmComment");
        String menuType = req.getParameter("menuType");
        String src = req.getParameter("src");
        String parentId = req.getParameter("parentId");
        
        if (StringUtils.isBlank(parentId)) {
			
        	parentId = "0";
        	
		}
        
        
        if(StringUtils.isBlank(mpid) 
        	|| StringUtils.isBlank(pmName) 
        	|| StringUtils.isBlank(pmCode)
        	|| StringUtils.isBlank(pmComment) 
        	|| StringUtils.isBlank(menuType))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else{
            MenuPrivilege menuPrivilege = new MenuPrivilege();
        	 if(!StringUtils.isBlank(src)){
            QueryMenuPrivilege qup = new QueryMenuPrivilege();
            menuPrivilege.setSrc(src);
            qup.setSrc(src);
            qup.setMpid(Integer.parseInt(mpid));
            int resultNum = menuService.checkMenuPrivilegeExist(qup);
            if(resultNum > 0 )
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("地址名重复");
                return mb;
            }
        	}
            menuPrivilege.setMpid(Integer.parseInt(mpid));
            menuPrivilege.setPmName(pmName);
            menuPrivilege.setPmCode(pmCode);
            menuPrivilege.setPmComment(pmComment);
            menuPrivilege.setMenuType(menuType);
            menuPrivilege.setParentId(Integer.parseInt(parentId));
            int resultNum1 = menuService.updateMenuResource(menuPrivilege);
            if(resultNum1 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("修改失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("修改成功");
            }
        }
        return mb;
    }
    
    @RequestMapping("deleteMenuResource")
    @ResponseBody
    public MessageBean deleteMenuPrivilege(HttpServletRequest req)
    {
        MessageBean mb = new MessageBean();
        String mpid = req.getParameter("mpid");
        if(StringUtils.isBlank(mpid))
        {
            mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
            mb.setMessage("表单信息不完整");
            return mb;
        }else
        {  
            int resultNum1 = menuService.deleteMenuResource(Integer.parseInt(mpid));
            if(resultNum1 < 0)
            {
                mb.setCode(StringHelper.MESSAGE_TYPE_ERROR);
                mb.setMessage("删除失败");
            }else{
                mb.setCode(StringHelper.MESSAGE_TYPE_SUCCESS);
                mb.setMessage("删除成功");
            }
        }
        return mb;
    }
}
